package org.itboys.store.service;

import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.itboys.store.entity.SearchTag;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 搜索标签管理
 */
@Service
public class StoreSearchTagService extends BaseService<SearchTag, Long> {

    @Resource(name = "productDS")
    private MongoDataSource ds;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return ds;
    }

    @Override
    protected Class<SearchTag> getEntityClass() {
        return SearchTag.class;
    }

    /**
     * 给商品追加标签
     * @param tags 标签集合
     * @param productId 商品ID
     */
    public void appendProductTag(List<String> tags, long productId) {
        if (CollectionUtils.isEmpty(tags)) {
            return;
        }
        for (String tag : tags) {
            if (StringUtils.isBlank(tag)) {
                continue;
            }
            SearchTag searchTag = getByField("tag" , tag);
            if (searchTag == null) {
                searchTag.setTag(tag);
                searchTag.setBrandIds(Collections.emptyList());
                searchTag.setCategoryIds(Collections.emptyList());
                searchTag.setProductIds(Lists.newArrayList(productId));
            } else {
                for(Long pid : searchTag.getProductIds()) {
                    if (pid == productId) {
                        continue;
                    }
                }
                searchTag.getProductIds().add(productId);
                super.update(searchTag.getId(), "productIds", searchTag.getProductIds());
            }
        }
    }

    /**
     * 给分类追加标签
     * @param tags 标签集合
     * @param categoryId 分类ID
     */
    public void appendCategoryTag(List<String> tags, long categoryId) {
        if (CollectionUtils.isEmpty(tags)) {
            return;
        }
        for (String tag : tags) {
            if (StringUtils.isBlank(tag)) {
                continue;
            }
            SearchTag searchTag = getByField("tag" , tag);
            if (searchTag == null) {
                searchTag.setTag(tag);
                searchTag.setBrandIds(Collections.emptyList());
                searchTag.setCategoryIds(Lists.newArrayList(categoryId));
                searchTag.setProductIds(Collections.emptyList());
            } else {
                for(Long cid : searchTag.getCategoryIds()) {
                    if (cid == categoryId) {
                        continue;
                    }
                }
                searchTag.getCategoryIds().add(categoryId);
                super.update(searchTag.getId(), "categoryIds", searchTag.getCategoryIds());
            }
        }
    }

    /**
     * 给品牌追加标签
     * @param tags 标签集合
     * @param brandId 品牌ID
     */
    public void appendBrandTag(List<String> tags, long brandId) {
        if (CollectionUtils.isEmpty(tags)) {
            return;
        }
        for (String tag : tags) {
            if (StringUtils.isBlank(tag)) {
                continue;
            }
            SearchTag searchTag = getByField("tag" , tag);
            if (searchTag == null) {
                searchTag.setTag(tag);
                searchTag.setCategoryIds(Collections.emptyList());
                searchTag.setBrandIds(Lists.newArrayList(brandId));
                searchTag.setProductIds(Collections.emptyList());
            } else {
                for(Long bid : searchTag.getBrandIds()) {
                    if (bid == brandId) {
                        continue;
                    }
                }
                searchTag.getBrandIds().add(brandId);
                super.update(searchTag.getId(), "brandIds", searchTag.getBrandIds());
            }
        }
    }

    /**
     * 找分类下的搜索标签
     * @param categoryId
     * @return
     */
    public List<SearchTag> getSearchTagListByCategoryId(long categoryId) {
        return hasThisOne("categoryIds",categoryId);
    }

    /**
     * 找分类关联的搜索标签
     * @param categoryId
     * @return
     */
    public List<String> getSearchTagsByCategoryId(long categoryId) {
        return getTags(getSearchTagListByCategoryId(categoryId));
    }

    /**
     * 找品牌下的搜索标签
     * @param brandId
     * @return
     */
    public List<SearchTag> getSearchTagListByBrandId(long brandId) {
        return hasThisOne("brandIds",brandId);
    }

    /**
     * 找品牌关联的搜索标签
     * @param brandId
     * @return
     */
    public List<String> getSearchTagsByBrandId(long brandId) {
        return getTags(getSearchTagListByBrandId(brandId));
    }

    /**
     * 找商品下的搜索标签
     * @param productId
     * @return
     */
    public List<SearchTag> getSearchTagListByProductId(long productId) {
        return hasThisOne("productIds",productId);
    }

    /**
     * 找商品下的搜索标签
     * @param productId
     * @return
     */
    public List<String> getSearchTagsByProductId(long productId) {
        return getTags(getSearchTagListByProductId(productId));
    }

    private List<String> getTags(List<SearchTag> tags) {
        if (CollectionUtils.isEmpty(tags)) {
            return Collections.emptyList();
        }
        return Lists.transform(tags, input -> input.getTag());
    }
}
